# -*- coding: utf-8 -*
__author__ = 'dell'
import math
from operator import itemgetter, attrgetter
import copy
#将静态环上的外卖订单单独形成连线，可以有剩余（最后处理），然后将静态环上各点依次插入到动态订单的连线上，要保证不超时，超时则去掉时间靠后的外卖订单

inputA = open('1.txt', 'r') #网点经纬度
inputB = open('2.txt', 'r') #配送点经纬度
inputC = open('3.txt','r') #商家经纬度
inputE = open('5_2.txt', 'r') #外卖订单
inputCycle = open('staticCycleTime.txt', 'r') #输入环路安排表
outputF = open('newCycleTime.txt', 'w') #新的环路
L={}

listE =[]
F=[]
A_set = []
for eachline in inputA.readlines():
    temp = eachline.strip().split(',')
    A_set.append(temp[0])
    L[temp[0]] = [float(temp[1]),float(temp[2])]
for eachline in inputB.readlines():
    temp = eachline.strip().split(',')
    L[temp[0]] = [float(temp[1]),float(temp[2])]
for eachline in inputC.readlines():
    temp = eachline.strip().split(',')
    L[temp[0]] = [float(temp[1]),float(temp[2])]


tempE=[]
n=1
for eachline in inputE.readlines(): #改成字典形式
    temp1 = eachline.strip().split('\t')
    tempE.append(temp1)
    if n%4==0:
        listE.append(tempE)
        tempE=[]
    n +=1


n=1
v=250
tempF=[]

for eachline in inputCycle.readlines():
    temp1 = eachline.strip().split('\t')
    tempF.append(temp1)
    if n%4==0:
        F.append(tempF)
        tempF=[]
    n +=1

def caldistance(point1,point2):
    dlon = (point1[0]-point2[0])/2
    dlat = (point1[1]-point2[1])/2
    lat1 = point1[1]
    lat2 = point2[1]
    R = 6378137
    s= 2*R*math.asin(((math.sin(dlat*math.pi/180))**2+math.cos(lat1*math.pi/180)*math.cos(lat2*math.pi/180)*(math.sin(dlon*math.pi/180))**2)**0.5)
    return s

def staytimeCal(p):
    time = int(round(3*(p**0.5)+5))
    return time

def addFirstPoint(eachF,Eset):
    return eachF



def dynamiclink(eachF):
    return eachF



restEandFandA =[]
restE=[]   #剩下的外卖订单
newF = []
AnumInNewF = {}

def getType(name):
    namelist = list(name)
    return namelist[0]

for i in range(len(F)):
    a=dynamiclink(F[i])
    restEandFandA=restEandFandA+a[1]
    newF.append(a)


listE.sort(key=itemgetter(3))

staticInNewF = []

for i in range(len(newF)):
    if len(newF[i][-1])%2==0:
        lon = L[newF[i][0][-2]][0]
        lat = L[newF[i][0][-2]][1]
        lastTime = int(newF[i][-1][-2])
    else:
        lon = L[newF[i][0][-1]][0]
        lat = L[newF[i][0][-1]][1]
        lastTime = int(newF[i][-1][-1])
    staticInNewF.append([i,lon,lat,lastTime])

def addrestrestE(eachRestE,newF):
    neworder = []
    for eachone in staticInNewF:
        distance = caldistance(L[eachRestE[0][0]],[eachone[1],eachone[2]])
        dtTime = int(eachRestE[3][0])-int(eachone[3])
        if dtTime>=0:
            neworder.append([dtTime+int(distance/270),eachone[0]])
    neworder.sort()
    insertorder = [x[1] for x in neworder]

    success = False

    for i in insertorder:
        if getType(newF[i][0][-1])=='A':
            newline00 = (newF[i][0][:])
            newline00.pop()
            newline11 = (newF[i][1][:])
            newline11.pop()
            newline22 = (newF[i][2][:])
            newline22.pop()
            newline33 = (newF[i][3][:])
            newline33.pop()
        else:
            newline00 = (newF[i][0][:])
            newline11 = (newF[i][1][:])
            newline22 = (newF[i][2][:])
            newline33 = (newF[i][3][:])
        dtime1 = int(eachRestE[3][0])-int(round(caldistance(L[newline00[-1]],L[eachRestE[0][0]])/v))-int(newline33[-1])

        if dtime1>=0:
            if int(newline33[0])==0:
                for j in range(len(newline33)):
                    newline33[j] = int(newline33[j])+dtime1
                newline00 = newline00 + eachRestE[0]
                newline11 = newline11 + eachRestE[1]
                newline22 = newline22 + eachRestE[2]
                newline33 = newline33 + eachRestE[3]
            else:
                medium = int(newline33[-1])+ int(round(caldistance(L[newline00[-1]],L[eachRestE[0][0]])/v))
                newline00 = newline00 + eachRestE[0]
                newline11 = newline11 + eachRestE[1]
                newline22 = newline22 + eachRestE[2]
                newline33 = newline33 + [medium]+eachRestE[3][1:]
            if int(newline33[-1])<=1000:
                newF[i][0] = newline00[:]
                newF[i][1] = newline11[:]
                newF[i][2] = newline22[:]
                newF[i][3] = newline33[:]
                staticInNewF[i] = [i,L[eachRestE[0][-1]][0],L[eachRestE[0][-1]][1],int(newF[i][3][-1])]
                success = True
                break
    if success==False:
        '''distanceSet = []
        for eachAdistance in A_set:
            distance = round(caldistance(L[eachAdistance],L[eachRestE[0][0]]))
            distanceSet.append([distance,eachAdistance])
        distanceSet.sort()
        Aname = distanceSet[0][1]'''
        newline0 = eachRestE[0]
        newline1 = eachRestE[1]
        newline2 = eachRestE[2]
        newline3 = eachRestE[3]
        newF.append([newline0,newline1,newline2,newline3])
        staticInNewF.append([len(newF)-1,L[eachRestE[0][-1]][0],L[eachRestE[0][-1]][1],newline3[-1]])

for everyRestE in listE:
    addrestrestE(everyRestE,newF)

for eachF in newF:
    if len(eachF[-1])%2==0:pass
    else:
        returnA = int(eachF[3][-1])+int(round(caldistance(L[eachF[0][-1]],L[eachF[0][0]])/v))
        eachF[0].append(eachF[0][0])
        eachF[1].append(eachF[0][0])
        eachF[2].append(0)
        eachF[3].append(returnA)

#将newF和restrestE写入到两个文件中去
for eachF in newF:
    for subeachF in eachF:
        for subsubeachF in subeachF:
            outputF.write('%s\t' %subsubeachF)
        outputF.write('\n')

Edemo = []
for everyF in newF:
    Edemo +=everyF[1]
print 3417-(len(list(set(Edemo)))-124-9214)

inputA.close()
inputB.close()
inputC.close()
inputE.close()
inputCycle.close()
outputF.close()